﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Pages_Employee_Manage_Stocks_EditStock : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["PinCode"] == null || Request.QueryString["DeliverableID"]==null)
            Response.Redirect("ViewStock.aspx");
        if (!DALStock.CheckDeliverable(Request.QueryString["DeliverableID"], Session["PinCode"].ToString()))
        {
            Response.Redirect("ViewStock.aspx");
        }
    }
    protected void FormViewEditStock_ItemCommand(object sender, FormViewCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {
            frmViewEditStock.DefaultMode = FormViewMode.Edit;
        }
        if (e.CommandName == "Submit")
        {
            SubmitInformation();
            
            
        }
        if (e.CommandName == "Cancel")
        {
            frmViewEditStock.DefaultMode = FormViewMode.ReadOnly;
        }
    }

    private void SubmitInformation()
    {
        if (Page.IsValid)
        {
            try
            {
                string pinCode = Session["PinCode"].ToString();
                string deliveryNumber = (frmViewEditStock.FindControl("txtDeliveryNumber") as TextBox).Text.Trim();
                string fromStock = (frmViewEditStock.FindControl("ddlFromStock") as DropDownList).SelectedValue.TrimEnd(' ');
                if (fromStock == null)
                {
                    fromStock = String.Empty;
                }
                string toStock = (frmViewEditStock.FindControl("ddlToStock") as DropDownList).SelectedValue.TrimEnd(' ');
                if (toStock == null)
                {
                    toStock = String.Empty;
                }
                string receivedDate = Convert.ToDateTime((frmViewEditStock.FindControl("txtReceivedDate") as TextBox).Text).ToString("yyyy-MM-dd");
                string sentDate = "";
                if ((frmViewEditStock.FindControl("txtSentDate") as TextBox).Text == "")
                {
                    sentDate = String.Empty;
                }
                if ((frmViewEditStock.FindControl("ckbStatus") as CheckBox).Checked && (frmViewEditStock.FindControl("txtSentDate") as TextBox).Text != "")
                {
                    sentDate = Convert.ToDateTime((frmViewEditStock.FindControl("txtSentDate") as TextBox).Text).ToString("yyyy-MM-dd");
                }
                if ((frmViewEditStock.FindControl("ckbStatus") as CheckBox).Checked &&(frmViewEditStock.FindControl("txtSentDate") as TextBox).Text == "")
                {
                    lblMessage.Text="Sent date must be filled when deliveried to customer";
                    return;
                }
                bool status = (frmViewEditStock.FindControl("ckbStatus") as CheckBox).Checked;
                if (DALStock.CheckDeliveryNumber(deliveryNumber))
                {
                    if (DALStock.DeleteDeliverableInStock(deliveryNumber, pinCode))
                    {
                        if (DALStock.AddDeliveralbeInStock(fromStock, toStock, receivedDate, sentDate, status, deliveryNumber, pinCode))
                        {
                            lblMessage.Text = "Update Successful";

                        }
                    }
                    else
                    {
                        lblMessage.Text = "Update Fail";
                    }
                }
                else
                {
                    lblMessage.Text = "Delivery Number doesn't exist. Please check again!";
                }
                Response.Redirect("EditStock.aspx?DeliverableID=" + deliveryNumber);
            }
            catch
            {
                lblMessage.Text = "Update Fail";
            }
        }
    }
}
